﻿using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Leo.Core.HttpApi.Host.HealthCheck
{
    public  class HttpApiHealthCheck: IHealthCheck
    {
        private bool healthCheckResultHealthy;

        public Task<HealthCheckResult> CheckHealthAsync(
            HealthCheckContext context,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            CheckHealthCheck();

            if (healthCheckResultHealthy)
            {
                return Task.FromResult(
                    HealthCheckResult.Healthy("A healthy result."));
            }

            return Task.FromResult(
                new HealthCheckResult(context.Registration.FailureStatus,
                "An unhealthy result."));
        }

        private void CheckHealthCheck()
        {
            healthCheckResultHealthy = true;
        }
    }
}
